from . import models
from rest_framework.generics import ListAPIView
from . import serializers

from utilss.response import APIResponse

# 排序，搜索过滤器
from rest_framework.filters import OrderingFilter, SearchFilter
# 自定义过滤器
from .filters import LimitFilter

# 分页过滤器
from .paginations import CoursePageNumberPagination, CourseLimitOffsetPagination, CourseCursorPagination

# 分类筛选：django-filter：filter_backends配置DjangoFilterBackend，再在filter_fields中配置分组筛选的字段
from django_filters.rest_framework import DjangoFilterBackend
# 自定义的区间筛序选过滤器
from .filters import CourseFilterSet


class FreeCourseListAPIView(ListAPIView):
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by('orders').all()
    serializer_class = serializers.FreeCourseModelSerializer
    # 配置过滤器类
    # filter_backends = [OrderingFilter, SearchFilter]
    # 自定义过滤器
    filter_backends = [OrderingFilter, SearchFilter, LimitFilter, DjangoFilterBackend]
    # 参与排序的字段: ordering=-price,id

    ordering_fields = ['price', 'id', 'students']

    # 参与搜索的字段: search=python  (name字段中带python就ok)
    search_fields = ['name', 'brief']

    # 自定义的分页器
    pagination_class = CoursePageNumberPagination
    # pagination_class = CourseLimitOffsetPagination
    # pagination_class = CourseCursorPagination

    # 参与分类筛序的字段，所有字段都可以，但是分组字段要有意义
    filter_fields = ["course_category"]

    filter_class = CourseFilterSet


class CategoriesListAPIView(ListAPIView):
    """
    课程分类标签
    """
    queryset = models.CourseCategory.objects.filter(is_delete=False, is_show=True).order_by("orders").all()
    serializer_class = serializers.CategoriesModelSerializer


from rest_framework.generics import RetrieveAPIView


class FreeCourseRetrieveAPIView(RetrieveAPIView):
    """获取当前免费课程的信息单个"""
    queryset = models.Course.objects.filter(is_delete=False, is_show=True).order_by("orders").all()

    serializer_class = serializers.FreeCourseModelSerializer


class ChapterListAPIView(ListAPIView):
    queryset = models.CourseChapter.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = serializers.CourseChapterModelSerializer
    # 过滤器 http://127.0.0.1:8000/course/chapters?course=2
    # 根据章节的课程id进行过滤
    filter_backends = [DjangoFilterBackend]
    # 过滤章节
    filter_fields = ['course']
